const app = getApp();
var {
  Api
} = require('../../api/api')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    subject:'',//笔记列表
    page:1,
    page_size:20,
    id:'',//题目id
    domain: Api.domain,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id:options.id,
    })
  },
  init3() { //笔记列表
    var that = this
    var params = {
      subject_ids: this.data.id,
      member_id:wx.$cache.get('member_id'),
      page: 1,
      page_size: this.data.page_size
    }
    wx.$http.get(wx.$get.note_list,params).then(res=>{
      console.log(res,'笔记列表')
      that.setData({
        subject: res
      })
    })
  },
  maskjl(){//记录笔记
    wx.navigateTo({
      url: '/pages/newnote/newnote?id='+this.data.id,
    })
  },
  pulldown() { //笔记列表
    this.setData({
      page:this.data.page+=1
    })
    var that = this
    var params = {
      subject_ids: this.data.id,
      member_id:wx.$cache.get('member_id'),
      page: this.data.page,
      page_size: this.data.page_size
    }
    wx.$http.get(wx.$get.note_list,params).then(res=>{
      console.log(res,'笔记列表')
      if(res.length==0){

      }else{
        let newadd = that.data.subject.concat(res)
        that.setData({
          subject: newadd
        })
      }
    })
  },
  love(e){//收藏笔记
    let {id} = e.currentTarget.dataset
    let {flage} = e.currentTarget.dataset
    let {index} = e.currentTarget.dataset
    var that = this
    var params = {
      log_id:id,
      type:1
    }
    wx.$http.get(wx.$get.give_log,params).then(res=>{
      console.log(res,'收藏笔记')
      if(flage==1){
        that.setData({
          [`subject[${index}].is_give`]:2
        })
      }else{
        that.setData({
          [`subject[${index}].is_give`]:1
        })
      }
      var pageArr = getCurrentPages();
      var thisPage = pageArr[pageArr.length - 2];
      if(thisPage.route=="pages/brushTopic/brushTopic"){
        thisPage.tidetail2()
      }
    })
  },
  kanimg(e){//看图片
    console.log(e)
    let {index} = e.currentTarget.dataset
    let {index2} = e.currentTarget.dataset
    console.log(this.data.subject[index].imgs)
    wx.previewImage({
      current: this.data.subject[index].imgs[index2], // 当前显示图片的http链接
      urls: this.data.subject[index].imgs // 需要预览的图片http链接列表
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.init3()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.pulldown()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return{
      title:wx.$cache.get('fx').title,
      imageUrl:wx.$cache.get('fx').img,
      path:`/pages/index/index?pid=${wx.$cache.get('member_id')}`
    }
  } 
})